package com.ewili.x.special.service.data.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.framework.utils.HttpClientUtils;
import com.ewili.framework.utils.ObjectHttpClientUtils;
import com.ewili.x.special.dao.inter.StockDao;
import com.ewili.x.special.dao.inter.ValueStockDao;
import com.ewili.x.special.entity.Stock;
import com.ewili.x.special.service.data.inter.ValueService;
import com.ewili.x.special.task.thread.inter.ValueThreadDaoTask;
import com.ewili.x.special.task.thread.inter.ValueThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wonder
 */
@Slf4j
@Service
public class ValueServiceImpl implements ValueService {

    @Autowired
    private StockDao stockDao;

    @Autowired
    private ValueStockDao valueStockDao;

    @Autowired
    private ValueThreadTask valueThreadTask;

    @Autowired
    private ValueThreadDaoTask valueThreadDaoTask;

    @Override
    public void spideValue() {
        List<Stock> stocks = stockDao.findAll();
        for (Stock stock : stocks) {
            this.spide(stock);
        }
    }

    @Override
    public void spide(Stock stock) {
        String baseUrl = "http://dcfm.eastmoney.com/EM_MutiSvcExpandInterface/api/js/get?type=GZFX_GGZB&token=894050c76af8597a853f5b408b759f5d&st=TRADEDATE&sr=-1&p=1&ps=100&js=var%20BVjHgvpO={pages:(tp),data:(x),font:(font)}&filter=(SECURITYCODE='" + stock.getCode() + "')&rt=52100197";

        Long page = ObjectHttpClientUtils.getEastMoneyJsonPages(baseUrl);

        if (page == null || page == 0) {
            return;
        }
        for (int i = 1; i <= page; i++) {
            String url = "http://dcfm.eastmoney.com/EM_MutiSvcExpandInterface/api/js/get?type=GZFX_GGZB&token=894050c76af8597a853f5b408b759f5d&st=TRADEDATE&sr=-1&p=" + i + "&ps=100&js=var%20BVjHgvpO={pages:(tp),data:(x),font:(font)}&filter=(SECURITYCODE='" + stock.getCode() + "')&rt=52100197";
            valueThreadTask.spideValue(url, stock.getCode());
        }
    }

    @Override
    public void spideValueStock() {

        valueStockDao.deleteAll();
        getValue();
        getValuePercent();
        getValeProperty();

    }

    private void getValue() {
        String baseUrl = "https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_VALUATIONSTATUS&sty=SECUCODE,TRADE_DATE,INDICATOR_TYPE,INDEX_VALUE,INDEX_PERCENTILE,VALATION_STATUS&callback=&extraCols=&p=1&ps=100&sr=&st=&token=&var=source=DataCenter&client=APP";

        Integer pages = ObjectHttpClientUtils.getEastMoneyPage(baseUrl);

        for (int i = 1; i <= pages; i++) {
            String url = "https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_VALUATIONSTATUS&sty=SECUCODE,TRADE_DATE,INDICATOR_TYPE,INDEX_VALUE,INDEX_PERCENTILE,VALATION_STATUS&callback=&extraCols=&p=" + i + "&ps=100&sr=&st=&token=&var=source=DataCenter&client=APP";
            String urlContent = HttpClientUtils.getContent(url, 30000);

            JSONObject contentObject = JSON.parseObject(urlContent);
            contentObject = contentObject.getJSONObject("result");

            JSONArray arrayObject = contentObject.getJSONArray("data");

            valueThreadDaoTask.saveValueStockData(arrayObject);
        }
    }

    private void getValeProperty() {
        String baseUrl= "https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_STOCKHISTORICALSURVEY&sty=SECUCODE,TRADE_DATE,INDEX_TYPE,DATE_TYPE,RISE_PROBABILITY,AVERAGE_INCREASE&callback=&extraCols=&p=1&ps=50&sr=&st=&token=&var=source=DataCenter&client=APP";
        Integer pages = ObjectHttpClientUtils.getEastMoneyPage(baseUrl);
        for (int i = 1; i <= pages; i++) {
            String urlProbablity = "https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_STOCKHISTORICALSURVEY&sty=SECUCODE,TRADE_DATE,INDEX_TYPE,DATE_TYPE,RISE_PROBABILITY,AVERAGE_INCREASE&callback=&extraCols=&p="+i+"&ps=50&sr=&st=&token=&var=source=DataCenter&client=APP";
            valueThreadTask.spideValueStockProbablity(urlProbablity);
        }
    }

    private void getValuePercent() {
        String baseUrl = "https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_STOCKVALUATIONTANTILE&sty=SECUCODE,STATISTICS_CYCLE,INDEX_TYPE,PERCENTILE_THIRTY,PERCENTILE_FIFTY,PERCENTILE_SEVENTY&callback=&extraCols=&p=1&sr=&st=&token=&var=source=DataCenter&client=APP";

        Integer pages = ObjectHttpClientUtils.getEastMoneyPage(baseUrl);

        for (int i = 1; i <= pages; i++) {
            String url = "https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_STOCKVALUATIONTANTILE&sty=SECUCODE,STATISTICS_CYCLE,INDEX_TYPE,PERCENTILE_THIRTY,PERCENTILE_FIFTY,PERCENTILE_SEVENTY&callback=&extraCols=&p=" + i + "&sr=&st=&token=&var=source=DataCenter&client=APP";
            String urlContent = HttpClientUtils.getContent(url, 30000);

            JSONObject contentObject = JSON.parseObject(urlContent);
            contentObject = contentObject.getJSONObject("result");

            JSONArray arrayObject = contentObject.getJSONArray("data");

            valueThreadDaoTask.saveValuePercentData(arrayObject);
        }
    }


}
